package com.j.lemon.learn.jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author lijunjun
 * @Date 2020/9/18 19:43
 * @Description
 */
public class DBUtils {
    private static final ConcurrentHashMap<String, DataSource> POOL = new ConcurrentHashMap<>();
//    private static final String DRIVER_NAME ="com.mysql.jdbc.Driver";
    private static final String DRIVER_NAME ="oracle.jdbc.driver.OracleDriver";
//    private static final String URL ="jdbc:mysql://10.21.171.220:3306/bj4a?verifyServerCertificate=false&autoReconnect=true&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true";
    private static final String URL ="jdbc:oracle:thin:@10.21.16.51:1521:lcfa";
    private static final String USERNAME ="dxnoc";
    private static final String PASSWORD ="dxnoc_123";
    private static final Integer MAX_ACTIVE =100;
    private static final Integer INITIAL_SIZE =50;
    private static final Integer MIN_IDLE =25;
    private static final Integer TIME_BETWEEN_EVICTION_RUNS_MILLIS =300;
    private static final Integer MAX_WAIT =9000;

    private static volatile DruidDataSource dataSource;
    public static DataSource getDataSource() {
        if(dataSource==null){
            synchronized (DataSourceUtils.class){
                if(dataSource==null){

                    DruidDataSource source = new DruidDataSource();
                    source.setDriverClassName(DRIVER_NAME);
                    source.setUrl(URL);
                    source.setUsername(USERNAME);
                    source.setPassword(PASSWORD);

                    source.setMaxActive(MAX_ACTIVE);
                    source.setInitialSize(INITIAL_SIZE);
                    source.setMinIdle(MIN_IDLE);
                    source.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION_RUNS_MILLIS);
                    source.setMaxWait(MAX_WAIT);
                    dataSource = source;
                }
            }
        }
        return dataSource;
    }
}
